﻿#include "stdafx.h"
#include "UserInit.h"

Cdmsoft *m_GlobalDM;//全局大漠
Cdmsoft* g_pDmTool;
Cdmsoft* g_pDmLD;
CString arrsAction[5] = { L"比色",L"找色",L"多点找色",L"找图",L"找字" };
CString arrsType[5] = { L"查找",L"单击",L"双击",L"滑动" };
CString arrTask[3] = { L"登录游戏",L"主线任务",L"日常任务" };
map<CString, int> mapTask;
map<CString, int> mapocr;
map<CString, OcrDetail> mapOcrDetail;
Range range;
map<CString, OcrDetail>ocrMap;
VARIANT _x, _y;
CString sTimeBmp;

void addArr(INT * nlist) {
	for (int i = 0; i < 10; i++)
	{
		nlist[i] = i;
	}
}

void SplitStr(CString strSrc, CString strGap, CStringArray &strResult)
{
	int nPos = strSrc.Find(strGap);
	CString strLeft = _T("");

	while (0 <= nPos)
	{
		strLeft = strSrc.Left(nPos);
		if (!strLeft.IsEmpty())
		{
			strResult.Add(strLeft);
		}

		strSrc = strSrc.Right(strSrc.GetLength() - nPos - strGap.GetLength());
		nPos = strSrc.Find(strGap);
	}

	if (!strSrc.IsEmpty())
	{
		strResult.Add(strSrc);
	}
}

int SplitVARIANT(CString source, VARIANT* nlist, CString divKey) {

	int j = 0; int Number = 0; int m_pos, d_len = divKey.GetLength();
	while (TRUE)
	{
		m_pos = source.Find(divKey, j);
		if (m_pos == -1 && j == 0) { break; }
		if (m_pos == -1 && j != 0)
		{
			nlist[Number].lVal = lVal(source.Mid(j, source.GetLength() - j));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
			break;
		}
		if (j == 0)
		{
			nlist[Number].lVal = lVal(source.Mid(0, m_pos));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
		}
		else {
			nlist[Number].lVal = lVal(source.Mid(j, m_pos - j));
			nlist[Number].lVal = nlist[Number].lVal < 0 ? 0 : nlist[Number].lVal;
		}
		j = m_pos + d_len;
		Number++;
	}
	return Number;
}


/*-----------------------数据类型转换-----------------------*/


//转char*(参数CString类型)[返回:char*]
const char* StrP(CString str)
{
	char *ptr;
#ifdef _UNICODE
	LONG len;
	len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
	ptr = new char[len + 1];
	memset(ptr, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, str, -1, ptr, len + 1, NULL, NULL);
#else
	ptr = new char[str.GetAllocLength() + 1];
	sprintf(ptr, _T("%s"), str);
#endif
	return ptr;

	//char szStr[1024] = { 0 };

	//wcstombs(szStr, str, str.GetLength());

	//const char * p = szStr;
	//return p;
	/*---方法1--------*/
	//int strLength = str.GetLength() + 1;
	//char *pValue = new char[strLength];
	//
	//strncpy(pValue, str, strLength);
	//return pValue;
	/*---方法2--------*/

	//char * ch_a = (char*)str.GetBuffer(str.GetLength());
	//return cstr;
	/*---方法3--------*/
	//   char *ch = str.GetBuffer(str.GetLength() + 1);
	//   str.ReleaseBuffer();
	//return ch;
	/*---方法4--------*/
	// 	int nLen = x.GetLength();
	// 		char* pStr;
	// 		pStr = new char[nLen + 1];
	// 		strcpy(pStr, x);
	// 		TRACE("%s\n", pStr);
	// 		return pStr;
}
//转CSring(参数int类型)[返回:CSring]
CString Str(const char* x) {
	CString str;
	str = x;
	return str;
}
CString Str(int x)
{
	CString str;
	str.Format(_T("%d"), x);
	return str;
}
//转CSring(参数LONG类型)[返回:CSring]
CString Str(long x)
{
	CString str;
	str.Format(_T("%d"), x);
	return str;
}
//转CSring(参数HWND类型)[返回:CSring]
CString Str(HWND x)
{
	CString str;
	str.Format(_T("%d"), x);
	return str;
}

HWND hWnd(CWnd * wnd)
{

	return wnd->m_hWnd;
	//or
	//return wnd.GetSafeHwnd(); //推荐
}
// CWnd *wnd(HWND hWnd)
// {
// 	return CWnd::FromHandle(hWnd);
//     //CWnd  *pWnd;
//     //pWnd->Attach(hWnd);
// }
//转CSring(参数HANDLE类型)[返回:CSring]
CString Str(HANDLE x)
{
	CString str;
	str.Format(_T("%d"), x);
	return str;
}
//转CSring(参数DWORD类型)[返回:CSring]
CString Str(DWORD x)
{
	CString str;
	str.Format(_T("%d"), x);
	return str;
}
//转CSring(参数BSTR类型)[返回:CSring]
CString Str(BSTR s)
{
	CString a(s);
	return a;
}
//转Bstr(参数cstring类型)[返回:Bstr]
BSTR StrBstr(CString s)
{
	BSTR a = s.AllocSysString();
	SysFreeString(a);
	return a;
}
//转Bstr(参数cahr*类型)[返回:Bstr]
BSTR StrBstr(char *x)
{
	CString s = Str(x);
	BSTR a = s.AllocSysString();
	SysFreeString(a);
	return a;
}
//转long(变参指针)[返回:long]
long  lVal(VARIANT x)
{

	return x.lVal;
}
//转long(CString字符串)[返回:long]
long  lVal(CString x)
{

	/*CString strTest = x;
	long l = 0;
	l = _ttol(strTest);
	return l;*/
	return _wtof(x);
}
float fVal(CString x) {
	return _wtof(x);
}
//转int(CString字符串)[返回:int]
int  iVal(CString x)
{

	/*CString strTest = x;
	int l = 0;
	l = _ttol(strTest);
	return l;*/

	return _wtoi(x);
}
//转long(字符数组或指针)[返回:long]
long lVal(char *s)
{
	return atoi(s);
}
//16进制字符串(长整数)[返回:CString]
CString Hex(long Data)
{
	TCHAR s[260];
	_ltow(Data, s, 16);

	return s;
}
//
//#include "MobileGameDlg.h"
//
//CIniFile g_IniFile;
//CIniFile g_AccountFile;
//CIniFile g_RoleNameFile;
//CIniFile g_MoneyFile;
//CIniFile g_TaskSpeedFile;
//CIniFile g_ChangHaoStateFile;//成号状态
//
//
//
//Cdmsoft *m_GlobalDM;//全局大漠
//Cdmsoft *m_DM[20];
//CLuck g_cLuck;
//
//CString m_SimulatorPath;
//CString g_Dnplayer2Path;
//CString g_strDesktopPath;
//
//
//
//
//ScriptSetupInfo m_ScriptSet[20];
//
//CString NumberToString(LONG Number)
//{
//	CString sTring;
//	sTring.Format(_T("%ld"), Number);
//	return sTring;
//}
//
//
//
//BOOL FolderExists(CString s)
//{
//	DWORD attr;
//	attr = GetFileAttributes(s);
//	return (attr != (DWORD)(-1)) &&
//		(attr & FILE_ATTRIBUTE_DIRECTORY);
//}
////创建一个目录
//BOOL CreateDir(CString P)
//{
//	int len = P.GetLength();
//	if (len < 2) return false;
//
//	if ('\\' == P[len - 1])
//	{
//		P = P.Left(len - 1);
//		len = P.GetLength();
//	}
//	if (len <= 0) return false;
//
//	if (len <= 3)
//	{
//		if (FolderExists(P))return true;
//		else return false;
//	}
//
//	if (FolderExists(P))return true;
//
//	CString Parent;
//	Parent = P.Left(P.ReverseFind('\\'));
//
//	if (Parent.GetLength() <= 0)return false;
//
//	BOOL Ret = CreateDir(Parent);
//
//	if (Ret)
//	{
//		SECURITY_ATTRIBUTES sa;
//		sa.nLength = sizeof(SECURITY_ATTRIBUTES);
//		sa.lpSecurityDescriptor = NULL;
//		sa.bInheritHandle = 0;
//		Ret = (CreateDirectory(P, &sa) == TRUE);
//		return Ret;
//	}
//	else
//		return FALSE;
//}
//
////分割字符串
//void StringSplit(CString source, CStringArray& dest, char division)
//{
//	if (source.IsEmpty())
//		return;
//	dest.RemoveAll();
//	for (int i = 0; i < source.GetLength(); i++)
//	{
//		if (source.GetAt(i) == division)
//		{
//			dest.Add(source.Left(i));
//			source.Delete(0, i + 1);
//			i = 0;
//		}
//	}
//	dest.Add(source);
//}
//
//
//
long RandomNumber(DWORD dwNumber1, DWORD dwNumber2)
{
	DWORD dwTemp;
	__asm
	{
		push ecx
		push edx
		rdtsc
		mov ecx, 0x17
		mul ecx
		add eax, 0x7
		mov ecx, dwNumber2//ss:[ebp + 0c]
		sub ecx, dwNumber1//ss : [ebp + 8]
		inc ecx
		xor edx, edx
		div ecx
		add edx, dwNumber1//ss : [ebp + 8]
		mov dwTemp, edx
		pop edx
		pop ecx
	}

	return dwTemp;
}
//
//DWORD WINAPI StartScript1()
//{
//	return 0;
//}
//
//DWORD WINAPI StartScript(LPVOID lParam)
//{
//	ScriptSetupInfo* pSSI = (ScriptSetupInfo*)lParam;
//
//	int iFreeDmIndex = pSSI->m_iDmIndex;
//	int iIndex = pSSI->m_iControlIndex;
//
//	OpenSimulator(iFreeDmIndex);
//
//	g_MobileGameDlg->InitUIGameInfo(iIndex, m_ScriptSet[iFreeDmIndex].m_GameInfoSt);//初始化一些信息
//
//
//	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 8, NumberToString(iFreeDmIndex));//模拟器序号
//
//	long hHandle = NULL;
//	CString strSimulatorTitleName;
//	if (iFreeDmIndex == 0)
//	{
//		strSimulatorTitleName = "雷电模拟器";
//	}
//	else
//	{
//		strSimulatorTitleName.Format("雷电模拟器-%d", iFreeDmIndex);
//	}
//
//	while (true)
//	{
//		hHandle =(long) ::FindWindow(NULL, strSimulatorTitleName);
//		DbgOut("[sdn] hHandle:%d", hHandle);
//		if ((int)hHandle)
//		{
//			break;
//		}
//		Sleep(3000);
//	}
//
//	//排序窗口
//	DbgOut("[sdn] hHandle:%d", hHandle);
//	//if (g_pPage1->m_radio_tran_expan.GetCheck())
//	//{
//	//	g_pPage1->OnBnClickedBtnHengxiang();
//	//}
//	//else
//	//{
//	//	g_pPage1->OnBnClickedBtnZhongxiang();
//	//}
//
//
//	g_pPage1->OnBnClickedBtnHengxiang();
//
//	BOOL bRet = RegisterDM(iFreeDmIndex);
//	if (!bRet)
//	{
//		AfxMessageBox("注册单个大漠失败");
//		return 0;
//	}
//
//	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 10, "运行中");
//	m_DM[iFreeDmIndex]->SetWindowState((long)hHandle, 1);
//
//	//long hGameHandle = GetSubWindow(hHandle);
//	long hGameHandle =  m_DM[iFreeDmIndex]->GetWindow(hHandle, 1);
//	DbgOut("[sdn] 子窗口句柄:%d", hGameHandle);
//	while (true)
//	{
//		Sleep(3000);
//		hGameHandle = m_DM[iFreeDmIndex]->GetWindow(hHandle, 1);
//		if (hGameHandle != 0)
//		{
//			break;
//		}
//		DbgOut("[sdn] ---子窗口句柄:%d", hGameHandle);
//	}
//
//	//m_ScriptSet[iFreeDmIndex].m_iDmIndex = iFreeDmIndex;//大漠序号
//	//m_ScriptSet[iFreeDmIndex].m_iControlIndex = iIndex;//控制台序号
//	m_ScriptSet[iFreeDmIndex].m_hGameHandle = hGameHandle;
//	//m_ScriptSet[iFreeDmIndex].m_hControlHandle = (long)g_MobileGameDlg->m_hWnd;//用于发送消息
//	DbgOut("[sdn] 绑定 le");
//
//	g_MobileGameDlg->m_list_info.SetItemText(iIndex, 1, NumberToString(hGameHandle));
//
//	//m_DM[iFreeDmIndex]->SetWindowState((long)hGameHandle, 1);
//
//	long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle,
//		"gdi", "windows3", "windows",
//		NULL,
//		0);
//
//	/*	long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle,
//	"gdi",
//	"windows",
//	"windows",
//	NULL,
//	0);*/
//	//long dm_ret = aDm[nID]->BindWindowEx(lHwnd,
//	//	"dx.graphic.opengl",
//	//	"dx.mouse.position.lock.api|dx.mouse.clip.lock.api|dx.mouse.input.lock.api|dx.mouse.state.api|dx.mouse.api|dx.mouse.cursor",
//	//	"dx.keypad.input.lock.api|dx.keypad.state.api|dx.keypad.api",
//	//	NULL,
//	//long dm_ret = m_DM[iFreeDmIndex]->BindWindowEx((long)hGameHandle, "dx.graphic.opengl", "windows", "windows", "dx.public.down.cpu", 0);
//	Sleep(2000);
//	if (dm_ret <= 0)
//	{
//		AfxMessageBox("游戏绑定失败!");
//		ClearOneDM(iFreeDmIndex);
//		return 0;
//	}
//
//	int iRate = g_IniFile.GetInt(_T("Set"), _T("CPU"));
//	if (iRate == 0)
//	{
//		iRate = 20;
//	}
//	DownCpu(iFreeDmIndex, iRate);
//	Sleep(5000);
//
//	CString strRoleNum = g_IniFile.GetString("Set", "账号角色");//2
//	int iRoleNum = 1;
//	if (iRoleNum == 0)
//	{
//		iRoleNum = 1;
//	}
//	while (true)
//	{
//		if (m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_iRoleIndex > iRoleNum)
//		{
//			DbgOut("[sdn] Here break1");
//			m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_iRoleIndex = 1;
//			break;
//		}
//		DbgOut("[sdn] 处理当前任务开始xxxxxxxxxxxxxxxxxxxx");
//		DealCurTaskProcess(iFreeDmIndex);
//
//		if (ChangeRole(iFreeDmIndex) != 1)
//		{
//			//开始下一个角色
//
//		}
//
//
//		Sleep(3000);
//	}
//
//	DbgOut("[sdn] 设置界面完成");
//	//设置界面完成
//	m_ScriptSet[iFreeDmIndex].m_GameInfoSt.m_strRunState = "完成";
//	ShowConsoleInfo(iFreeDmIndex, 10);
//	//设置配置文件完成
//	CString strSet;
//	strSet.Format("%d", m_ScriptSet[iFreeDmIndex].m_iControlIndex + 1);
//	g_AccountFile.SetInt(strSet, "完成", 1);//Account.ini
//
//										  /*DbgOut("[sdn] ---------------开始下一个账号----------------");
//										  ::SendMessage((HWND)m_ScriptSet[iFreeDmIndex].m_hControlHandle, WM_USERMONITOR, 2, (LPARAM)iFreeDmIndex);
//										  for (int N = 0; N<30; N++)
//										  {
//										  Sleep(2000);
//										  }*/
//
//	return 0;
//}

bool FindCol(OcrDetail ocrDetail) {
	VARIANT nPoint[4];
	SplitVARIANT(ocrDetail.sPoint, nPoint, L",");
	g_pDmLD->FindColor(nPoint[0].lVal, nPoint[1].lVal, nPoint[2].lVal, nPoint[3].lVal, ocrDetail.sOcrText, 0.9, 0, &_x, &_y);
	if (_x.lVal == -1) {
		return false;
	}
	else {
		return ocrType(ocrDetail);
	}
}

bool FindMulCol(OcrDetail ocrDetail) {

	VARIANT nPoint[4];
	SplitVARIANT(ocrDetail.sPoint, nPoint, L",");

	CString str;
	str = ocrDetail.sOcrText;
	int nIndex = str.Find(L",");
	str = str.Mid(0, nIndex);
	g_pDmLD->FindMultiColor(nPoint[0].lVal, nPoint[1].lVal, nPoint[2].lVal, nPoint[3].lVal, str, ocrDetail.sOcrText, 0.9, 0, &_x, &_y);
	if (_x.lVal == -1) {
		return false;
	}
	else {
		return ocrType(ocrDetail);
	}
}

bool CmpCol(OcrDetail ocrDetail) {

	CStringArray arrStr;
	SplitStr(ocrDetail.sOcrText, L"|", arrStr);
	BOOL bl = false;
	for (int i = 0; i < arrStr.GetCount(); i++)
	{
		CStringArray arrStr1;
		SplitStr(arrStr[i], L",", arrStr1);
		CString str1 = arrStr1[0];
		CString str2 = arrStr1[1];
		CString str3 = arrStr1[2];
		if (g_pDmLD->CmpColor(lVal(arrStr1[0]), lVal(arrStr1[1]), arrStr1[2], 0.95) == 1) { //未匹配
			bl = true;
		}
		else {
			if (i == 0) {

				_x.lVal = _wtol(arrStr1[0]);
				_y.lVal = _wtol(arrStr1[1]);
			}

		}
	}
	if (bl) {
		return false;
	}
	else {
		return ocrType(ocrDetail);
	}
}

bool FindPic(OcrDetail ocrDetail) {
	VARIANT nPoint[4];
	CString str = ocrDetail.sPoint;
	SplitVARIANT(str, nPoint, L",");

	//CString str;
	str.Format(L"%s%s", ocrDetail.sName, L".bmp");
	VARIANT m_picData, m_picSize;
	//	CString pic_info = "";
	//	pic_info = g_pDmLD->AppendPicAddr(pic_info, m_picData.lVal, m_picSize.lVal);
	//g_pDmLD->FindPicMem(nPoint[0].lVal - 3, nPoint[1].lVal - 3, nPoint[2].lVal + 3, nPoint[3].lVal + 3, ocrDetail.sOcrText, "202020", 0.95, 0, &_x, &_y);
	g_pDmLD->FindPic(nPoint[0].lVal - 3, nPoint[1].lVal - 3, nPoint[2].lVal + 3, nPoint[3].lVal + 3, str, L"050505", 0.90, 0, &_x, &_y);

	if (_x.lVal == -1)
		return false;
	else {
		return ocrType(ocrDetail);
	}
}

bool FindStr(OcrDetail ocrDetail) {
	VARIANT nPoint[4];
	SplitVARIANT(ocrDetail.sPoint, nPoint, L",");

	g_pDmLD->FindStrFast(nPoint[0].lVal - 3, nPoint[1].lVal - 3, nPoint[2].lVal + 3, nPoint[3].lVal + 3, ocrDetail.sName, ocrDetail.sOcrText, 0.9, &_x, &_y);
	if (_x.lVal == -1)
		return false;
	else {
		return ocrType(ocrDetail);
	}
}

bool(*funOcrType[])(OcrDetail) = { dmClick,dmDoubleClick,dmSlide, dmDoubleClick };

bool ocrType(OcrDetail ocrDetail) {
	CString str;
	int nType = iVal(ocrDetail.sType);
	if (nType == 0) {
		if (_x.lVal > 0)
			return true;
		else
			return false;
	}
	nType--;
	funOcrType[nType](ocrDetail);
	return Continued(ocrDetail);
}


bool dmClick(OcrDetail ocrDetail) {
	VARIANT nOffset[2] = { 0,0 };
	SplitVARIANT(ocrDetail.sOffset, nOffset, L",");
	g_pDmLD->MoveToEx(_x.lVal + nOffset[0].lVal, _y.lVal + nOffset[1].lVal, 5, 5);
	g_pDmLD->LeftClick();
	g_pDmLD->delay(150);
	return true;
}
bool dmDoubleClick(OcrDetail ocrDetail) {
	//g_pDmLD->MoveToEx(ocrInfo.OffsetX, ocrInfo.OffsetY, 5, 5);
	g_pDmLD->delay(100);
	g_pDmLD->LeftClick();
	g_pDmLD->delay(150);
	g_pDmLD->LeftClick();
	return true;
}
bool dmSlide(OcrDetail ocrDetail) {
	return true;
}
bool dmContinued(OcrDetail ocrDetail) {
	return true;
}

//bool(*funContinued[])(CString,CString) = { dmClick,dmDoubleClick,dmSlide, dmDoubleClick };

bool ContinuedAction(int type, CString str) {

	int nSize;
	if (type == 1) {
		long x, y;
		x = _wtol(str.Tokenize(L",", nSize));
		y = _wtol(str.Tokenize(L",", nSize));
		g_pDmLD->Click(x, y);
	}
	else if (type == 2) {

	}
	else if (type == 3)
	{
		long m_SimHwnd = m_GlobalDM->FindWindow(L"LDPlayerMainFrame", L"雷电模拟器");
		m_SimHwnd = m_GlobalDM->GetWindow(m_SimHwnd, 1);
		g_pDmLD->SendString2(m_SimHwnd, str);
	}
	return true;
}

//分割数据处理 1:移动点击,2:滑动处理,3:字符串输入
bool Continued(OcrDetail ocrDetail) {
	//格式 1:x,y|x,y@2:abc@3
	int m2;
	CString m3;
	int nStart1 = 0;

	if (ocrDetail.sEnlarge != L"") {
		int istart = 0;
		if (ocrDetail.sEnlarge.Find(L"@") > 0) {
			while (istart == -1) {//分割 @符号
				CString m;
				m = ocrDetail.sEnlarge.Tokenize(L"@", istart);//1:1,2,3|1,2,3@2:1,2|1,2@3:1,2|3,3
				if (m != "" || m.GetLength() > 0) {
					int nStart = 0;
					while (nStart == -1)//分割 |符号
					{
						CString m1;
						m1 = m.Tokenize(L"|", nStart); //1:1, 2, 3 | 1, 2, 3
						if (m1 != L"" || m1.GetLength() > 0) {
							//第1组数据
							m2 = iVal(m1.Tokenize(L":", nStart1));
							m3 = m1.Tokenize(L":", nStart1);
							ContinuedAction(m2, m3);
						}
					}
				}
			}
		}
		else {
			m2 = iVal(ocrDetail.sEnlarge.Tokenize(L":", nStart1));
			m3 = ocrDetail.sEnlarge.Tokenize(L":", nStart1);
			ContinuedAction(m2, m3);
		}

	}
	g_pDmLD->delay(1500);
	return true;
}

long randNum(long n) {
	srand((unsigned)time(NULL));
	long r = rand() % n;
	return r;

}

void randMove(long x, long y) {
	//g_pDmLD->MoveDD();
	g_pDmLD->MoveTo(randNum(x), randNum(y));
}

bool runScript(CString str) {
	bool(*pn[])(OcrDetail) = { CmpCol,FindCol,FindMulCol,FindPic,FindStr };
	OcrDetail ocrDetail= ocrMap[str];
	int nAction = iVal(ocrDetail.sAction);
	return pn[nAction](ocrDetail);
}